package com.liuyu.common.framework.iinsure.redis;

import com.liuyu.common.framework.iinsure.redis.serializer.GenericJackson2JsonRedisSerializerEx;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConf
 * @Author: liuyu
 * @Date: Created in 2020/5/16 8:30
 * @Version: 1
 * @Modified By:
 */
@Configuration
public class RedisConf implements InitializingBean {

    @Autowired
    private RedisTemplate redisTemplate;

    public void init() {
        // 解决key的序列化方式，使用String。
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(redisSerializer());
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(redisSerializer());
        redisTemplate.setDefaultSerializer(redisSerializer());
        redisTemplate.afterPropertiesSet();
    }

    @Bean
    public GenericJackson2JsonRedisSerializerEx redisSerializer() {
        //自定义序列化方式
        GenericJackson2JsonRedisSerializerEx genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializerEx();
        return genericJackson2JsonRedisSerializer;
    }

    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        init();
    }
}
